/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
*/

package com.chasekernan.hxnova.core.dataholders;

import com.chasekernan.hxnova.core.stars.Star;

/**
    Holds only the location, id, and name of a star and is distributed at the 
    start of the game.
**/
class Map {
    
    public var stars : Array<Star>;
    
    public function new() {
        stars = new Array();
    }
    
    public function addStar(s : Star) {
        var s2 : Star = s.cloneMapPart();
        s2.id = stars.length;
        stars.push(s2);
    }
    
    public function addStars(stars : Array < Star > ) {
        for (s in stars) addStar(s);
    }
    
    public static var XML_TITLE = "map";
    
    public static function writeToXml(map : Map) : Xml {
        var xml : Xml = Xml.createElement(XML_TITLE);
        for (s in map.stars) {
            xml.addChild(Star.writeMapPartToXml(s));
            //TODO: FIGURE OUT SPACER PROBLEM
            xml.addChild(Xml.createElement("spacer"));
        }
        
        return xml;
    }
    
    public static function readFromXml(xml : Xml) : Map {
        try {
            var m : Map = new Map();
            for (e in xml.elementsNamed(Star.XML_TITLE)) {
                var s = Star.readMapPartFromXml(e);
                m.stars[s.id] = s;
            }
            return m;
        } catch (e : Dynamic) {
            throw "Error while reading map from xml: " + e;
        }
    }
    
    
}